package com.kadron.validator;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import org.jboss.seam.core.Interpolator;

public class KPositiveValidator implements Validator {

	@Override
	public void validate(FacesContext facesContext, UIComponent component, Object value)
			throws ValidatorException {
		try {			
			Integer quantity = (Integer) value;
			
			if (quantity == null || quantity <= 0) {
				((UIInput)component).setValid(false);
				throw new ValidatorException(getMessage());
//				facesContext.addMessage(component.getClientId(facesContext), getMessage());
			}
		} catch (Exception e) {
			throw new ValidatorException(getMessage());
		}
	}
	
	private FacesMessage getMessage() {
		String msg = Interpolator.instance().interpolate("#{messages['value_must_be_positive_number']}", new Object());
		FacesMessage message = new FacesMessage(msg);
		return message;
	}
}
